// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var leftMenu = require("../leftMenu/leftMenu");
	var js_platform_main = require("./platform_main.js");
	var constant = require("constant");
	var router = require("../hash/pageRouter");
	var databind = require("databind");
	var css_detail = require("./pickupPreDetail.css");
	var tools = require("tools");
	var pickupId;
	var html_oppose_pickup = require("./html_opposePickupPreApplication.html");
	var init = function(page) {
		pickupId = page.params.pickupId;
		inquirePrePickupApplicationDetail();
//		leftMenu.init(6);
//		js_platform_main.init(0);
		$(".left").removeClass("none");
		initEvent();
		router.init();
		
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//拒绝提货申请
		$("#oppasePrePickup").on("click", function(){
			$("#popdialog").html(html_oppose_pickup);
			dialog.showDialog($("#oppose_pickup_pre_dialog"));
			$("#oppose_pickup_pre_Id").val(pickupId);
			tools.inquireCommonSelectorList(9, $(".applicantReson"));
			$("#opposePickupPreCancel").unbind("click").click(function(){
				dialog.hideDialog($("#oppose_pickup_pre_dialog"));
			});
			$("#opposePickupPreSure").unbind("click").click(function(){
				databind.submitDom($("#oppose_pickup_pre_dialog"), function(){
					base.alert(1, "拒提前还款成功！");
					dialog.hideDialog($("#oppose_pickup_pre_dialog"));
					router.route("main/pickupList.html");
				});
			});
		});
		
		//同意提货申请
		$("#consentPrePickup").on("click", function(){
			router.route("main/pickupPreConsent.html?pickupId="+pickupId);
		});
	};

	//提前还款详情
	var inquirePrePickupApplicationDetail = function() {
		base.fnAjax({
			action: "inquirePrePickupApplicationDetail.json",
			oData: {
				pickupId: pickupId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					databind.fillForm($("#pickupPreDetail"),data.datas[0]);
					if($("#pickupPreDetailDeliveryMethod").html()==2){
						$("#pickupPreDetailDeliveryMethod").html("到港");
					}else if($("#pickupPreDetailDeliveryMethod").html()==1){
						$("#pickupPreDetailDeliveryMethod").html("到门");
					}
					
					if($("#pickupPreDetailPickupMethod").html()==1){
						$("#pickupPreDetailPickupMethod").html("现金提货");
					}else if($("#pickupPreDetailPickupMethod").html()==2){
						$("#pickupPreDetailPickupMethod").html("换货");
					}
					
					juicer.register("getValueByKey", constant.getValueByKey);
					
					var tpl1 = $("#pickupPreDetailBulkstockTpl").html();
					var html1 = juicer(tpl1, data);
					$("#pickupPreDetailBulkstockBody").html(html1);
					
					var tpl2 = $("#pickupPreDetailInventoryTpl").html();
					var html2 = juicer(tpl2, data);
					$("#pickupPreDetailInventoryBody").html(html2);
//					
					if($("#pickupPreDetailInventoryBody").find("tr").length==1){
						$("#pickupPreDetailInventoryBody").parent().addClass("none");
					};
					if($("#pickupPreDetailBulkstockBody").find("tr").length==1){
						$("#pickupPreDetailBulkstockBody").parent().addClass("none");
					};
					
					$("#pickupPreDetailInventoryBody").on("click", ".detail", function(){
						router.route("bulkstockTallyDetail/bulkstockTallyDetail.html?inventoryId="+$(this).parents("tr").attr("inventoryId"));
					});
					
					inquireFinanceApplicationDetail(data.datas[0].financeId);
					//得到货箱总估值
					$.each($(".bulkstockValuations"), function(i, val) {
							var bulkstockValuations = 0;
							$.each($(this).parents("tr").find(".bulkstockValuation"), function(i, val) {
								bulkstockValuations = bulkstockValuations + parseFloat($(this).html());
							});
							$(this).html(bulkstockValuations);
						});
						
						//得到货箱总应付值
					$.each($(".financeValuations"), function(i, val) {
							var financeValuations = 0;
							$.each($(this).parents("tr").find(".financeValuation"), function(i, val) {
								financeValuations = financeValuations + parseFloat($(this).html());
							});
							$(this).html(financeValuations);
						});
				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	}
	//融资基本信息
	var inquireFinanceApplicationDetail = function(financeId){
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			oData: {
				financeId: financeId
			}, //传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
				var interestDate = data.datas[0].interestDate;
				var loanTimeLimit = data.datas[0].loanTimeLimit;
				var str = interestDate.replace(/-/g,"/");
				var oldDeliveryDate = addDate(str,loanTimeLimit);
				$(".oldDeliveryDate").html(oldDeliveryDate);
				
				} else {
					base.alert(3, "信息获取失败");
				}
			}
		});
	}
	var addDate = function(date,days){
		  var d=new Date(date);
       d.setDate(d.getDate()+days);
       var m=d.getMonth()+1;
       return d.getFullYear()+'-'+m+'-'+d.getDate();
	}
	 
	return {
		init: init
	};
});